﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using IConfig;

namespace VideoConfig
{
    public class FileConfigInfo : IConfigInfo
    {
        readonly Dictionary<string, string> configInfo;

        public FileConfigInfo(FileInfo file)
        {
            //TODO:读取配置文件
            this.configInfo = new Dictionary<string, string>();
            string config = string.Empty;
            FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs, Encoding.Default);
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            int index;
            while (true)
            {
                config = sr.ReadLine();

                if (config == null) break;

                if (!config.Contains("=")) continue;

                config = config.Replace(" ", "");
                config = config.Replace("\t", "");
                index = config.IndexOf("//");
                if (index >= 0)config = config.Remove(index);

                if (config.Length < 3) continue;
                
                string[] keyvalue = config.Split('=');

                if (keyvalue.Length == 2 && !configInfo.ContainsKey(keyvalue[0].ToLower())) 
                    configInfo.Add(keyvalue[0].ToLower(), keyvalue[1]);
            }
            sr.Close();
            fs.Close();
        }

        #region IConfigInfo 成员
        public string this[string parameter]
        {
            get 
            {
                if (configInfo.ContainsKey(parameter.ToLower()))
                    return configInfo[parameter.ToLower()];
                else 
                    return string.Empty;
            }
        }

        #endregion
    }
}
